const Sequelize = require('sequelize')

const sequelize = require('../db');

const Model = sequelize.define('stat_error', 
  {
      url: { type: Sequelize.STRING },
      time: { type: Sequelize.TIME, },
      sub_type: { type: Sequelize.STRING, },
      message: { type: Sequelize.STRING, },
      stack: { type: Sequelize.STRING, },
      row: { type: Sequelize.INTEGER, },
      col: { type: Sequelize.INTEGER, },
      ip: { type: Sequelize.STRING, },
      ua: { type: Sequelize.STRING, },
  },
  {
    tableName: 'stat_error',
    // 不要忘记启用时间戳！
    timestamps: true,

    // 不想要 false
    createdAt: 'create_time',

    // 想要 updatedAt 但是希望名称叫做 updateTimestamp
    updatedAt: false
  }
);

module.exports.add = async (data) => await Model.create(data)
module.exports.addBatch = async (datas) => await Model.bulkCreate(datas)

module.exports.del = async (id) => await Model.destroy({ where: { id } })


module.exports.update = async (data) => {
  let newUser = {...data}
  delete newUser['id']
  return await Model.update({ ...newUser }, {
      where: { id: data.id }
  })
}

module.exports.findOne = async (condition) => {
  let users = await Model.findAll({ where: condition })
  return users[0]
}

module.exports.findAll = async (condition = {}) => {
  return await Model.findAll({ where: condition })
}

module.exports.getErrorList = async ({limit=10, offset=0}) => {
    return await Model.findAndCountAll({
        limit,
        offset,
        order: [['time', 'DESC']]
    })
}